﻿using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;

namespace Kira.Compiler.Bods.Test
{
    [TestClass]
    public class DataIntegratorTest
    {
        private String baseDir = (new DirectoryInfo(Assembly.GetExecutingAssembly().Location)).Parent.FullName + "\\Bods Files";

        [TestMethod]
        public void TestSSISQL()
        {
            TestSQL("SSI");
        }

        [TestMethod]
        public void TestBACKFILLSQL()
        {
            TestSQL("BACKFILL");
        }

        [TestMethod]
        public void TestBACKFILL_CTRLSQL()
        {
            TestSQL("BACKFILL_CTRL");
        }

        [TestMethod]
        public void TestCORESQL()
        {
            TestSQL("CORE");
        }

        [TestMethod]
        public void TestEXTSQL()
        {
            TestSQL("EXT");
        }

        [TestMethod]
        public void TestLOAD_CONFIGSQL()
        {
            TestSQL("LOAD_CONFIG");
        }

        [TestMethod]
        public void TestOTHERSQL()
        {
            TestSQL("OTHER");
        }

        [TestMethod]
        public void TestPREPROCESSINGSQL()
        {
            TestSQL("PREPROCESSING");
        }

        [TestMethod]
        public void TestRESTEAMSQL()
        {
            TestSQL("RESTEAM");
        }

        [TestMethod]
        public void TestSOURCE_EXTRACTSQL()
        {
            TestSQL("SOURCE_EXTRACT");
        }

        [TestMethod]
        public void TestSSNPSQL()
        {
            TestSQL("SSNP");
        }

        [TestMethod]
        public void TestTSNPSQL()
        {
            TestSQL("TSNP");
        }

        private void TestSQL(String folder)
        {
            DirectoryInfo dir = new DirectoryInfo(baseDir + "\\" + folder);
            foreach (var file in dir.GetFiles("*.xml"))
            {
                XElement xml = XElement.Load(file.FullName);
                DataIntegrator di = BodsElementFactory.Create(xml) as DataIntegrator;
                StreamWriter sw = new StreamWriter(File.OpenWrite(file.FullName + ".sql"));
                foreach (var df in di.GetElements<DIDataflow>())
                {
                    sw.WriteLine(df.ToString());
                }
                sw.Close();
            }
        }

        private void TestStats(String folder)
        {
            DirectoryInfo dir = new DirectoryInfo(baseDir + "\\" + folder);
            foreach (var file in dir.GetFiles("*.xml"))
            {
                XElement xml = XElement.Load(file.FullName);
                DataIntegrator di = BodsElementFactory.Create(xml) as DataIntegrator;
                StringBuilder str = new StringBuilder();
                RecursiveStats(di, 0, str);
                StreamWriter sw = new StreamWriter(File.OpenWrite(file.FullName + ".stats"));
                sw.WriteLine(str.ToString());
                sw.Close();
            }
        }

        private void RecursiveStats(BodsElementBase ele, int level, StringBuilder str)
        {
            str.Append(new string('-', level * 4))
                .Append("(").Append(level).Append(")")
                .Append(ele.ElementName)
                .Append("[").Append(ele.Elements.Count()).AppendLine("]");
            foreach (var c in ele.Elements)
            {
                RecursiveStats(c, level + 1, str);
            }
        }

        [TestMethod]
        public void TestBackfill()
        {
            TestStats("BACKFILL");
        }

        [TestMethod]
        public void TestBackfillCtrl()
        {
            TestStats("BACKFILL_CTRL");
        }

        [TestMethod]
        public void TestCore()
        {
            TestStats("CORE");
        }

        [TestMethod]
        public void TestExt()
        {
            TestStats("EXT");
        }

        [TestMethod]
        public void TestLoadConfig()
        {
            TestStats("LOAD_CONFIG");
        }

        [TestMethod]
        public void TestOther()
        {
            TestStats("OTHER");
        }

        [TestMethod]
        public void TestPREPROCESSING()
        {
            TestStats("PREPROCESSING");
        }

        [TestMethod]
        public void TestRESTEAM()
        {
            TestStats("RESTEAM");
        }

        [TestMethod]
        public void TestSOURCE_EXTRACT()
        {
            TestStats("SOURCE_EXTRACT");
        }

        [TestMethod]
        public void TestSSI()
        {
            TestStats("SSI");
        }

        [TestMethod]
        public void TestSSNP()
        {
            TestStats("SSNP");
        }

        [TestMethod]
        public void TestTSNP()
        {
            TestStats("TSNP");
        }
    }
}
